//
//  GBInterMaster_13001Logic.m
//  NetworkTest
//
//  Created by xiaosi on 16/7/29.
//  Copyright © 2016年 xiaosi. All rights reserved.
//

#import "GBInterMaster_DeviceLogic13001.h"
#import "SampuxGetCurrentTime.h"

@class GBInterMaster_Launcher;

@implementation GBInterMaster_DeviceLogic13001

+ (int) responseDeviceConfig:(GBInterMaster_NetData*)netData{
    int result = 0x01;
    if (netData.bodyData.length < 19) {
        return 0x01;
    }
    int offset = 0;
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    result = bytes[offset];
    offset += 1;
    NSString* account;
    int isDataSyn;
    NSString* ownAccount = @"";
    int isPossessor;
    NSString* alias = @"";
    if(result == 0x00){
    @try {
        account = [[NSString alloc] initWithBytes:bytes+offset length:16 encoding:NSUTF8StringEncoding];
        offset += 16;
        isDataSyn = bytes[offset];
        offset += 1;
        int len = bytes[offset];
        offset += 1;
        if(len > 0){
            ownAccount = [[NSString alloc] initWithBytes:bytes+offset length:len encoding:NSUTF8StringEncoding];
        }
        offset += len;
        isPossessor = bytes[offset];
        offset += 1;
        len = bytes[offset];
        offset += 1;
        alias = @"";
        if(len > 0){
            alias = [[NSString alloc] initWithBytes:bytes+offset length:len encoding:NSUTF8StringEncoding];
        }
        offset += len;
         NSLog(@"offset:%d,length:%ld",offset,netData.bodyData.length);
        if(netData.bodyData.length != offset){
            result = 0x01;
        }
    }
    @catch (NSException *exception) {
        result = 0x01;
    }
    }
    NSDictionary* userData;
    if(result == 0x00){
        userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result],@"deviceAccount":account,@"isDataSyn":[NSNumber numberWithInt:isDataSyn],@"ownAccount":ownAccount,@"isPossessor":[NSNumber numberWithInt:isPossessor],@"alias":alias};
    }else{
        userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result],@"deviceAccount":account};
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_DEVICE_CONFIG object:nil userInfo:userData];
    return result;
    
}

+ (int) responseDeviceRunRecord:(GBInterMaster_NetData*)netData{
    if (netData.bodyData.length < 19) {
        return 0x01;
    }
    int offset = 0;
    Byte* bys = (Byte*)[netData.bodyData bytes];
    int result = bys[offset];
    offset += 1;
    NSString* account = [[NSString alloc] initWithBytes:bys+offset length:16 encoding:NSUTF8StringEncoding];
    offset += 16;
    int tag = bys[offset] != 0x00 ? 0xff : 0x00;
    offset += 1;
    int count = bys[offset];
    offset += 1;
    GBInterMaster_Bean_DeviceInfo13001* device = nil;
    unsigned long tm = 0;
    NSMutableArray *records = [NSMutableArray arrayWithCapacity:0];
    if(result == 0x00){
        @try {
            for (int i = 0; i < count; ++i) {
                device = [[GBInterMaster_Bean_DeviceInfo13001 alloc] init];
                device.account = account;
                tm = (long)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00)+ ((bys[offset + 2] & 0x0000FF) << 16 & 0xFF0000)+ ((bys[offset + 3] & 0x000000FF) << 24 & 0xFF000000));
                offset += 4;
                device.stateTime = tm;
                device.powerSwitch = bys[offset] & 0x00FF;
                offset += 1;
                device.workMode = bys[offset] & 0x00FF;
                offset += 1;
                device.isHeat = (bys[offset] & 0x00FF) == 1?true:false;
                offset += 1;
                device.heatMode = bys[offset] & 0x00FF;
                offset += 1;
                device.pragramCurrentPoint = bys[offset] & 0x00FF;
                offset += 1;
                device.currentRoomTempe =(short)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00));
                offset += 2;
                device.currentRoomHumidity =(bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
                offset += 2;
                device.currentSetTempe =(short)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00));
                offset += 2;
                device.QKHSetTemp =(short)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00));
                offset += 2;
                device.CFTSetTemp =(short)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00));
                offset += 2;
                device.ECSetTemp =(short)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00));
                offset += 2;
                device.ProgramSetTemp =(short)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00));
                offset += 2;
                device.tempSetLimitTop =(short)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00));
                offset += 2;
                device.tempSetLimitLown =(short)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00));
                offset += 2;
                tm = (long)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00)+ ((bys[offset + 2] & 0x0000FF) << 16 & 0xFF0000)+ ((bys[offset + 3] & 0x000000FF) << 24 & 0xFF000000));
                offset += 4;
                device.actionTotalTime = tm;
                device.isSiliconControlProtection = (bys[offset] & 0x00FF) == 1?true:false;
                offset += 1;
                device.isOverTempProtection = (bys[offset] & 0x00FF) == 1?true:false;
                offset += 1;
                device.isColdTempProtection = (bys[offset] & 0x00FF) == 1?true:false;
                offset += 1;
                [records addObject:device];
            }
        }
        @catch (NSException *exception) {
            result = 0x01;
        }
    }
    NSDictionary* userData;
    if(result == 0x00){
        userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result],@"tag":[NSNumber numberWithInt:tag],@"runRecords":records,@"deviceAccount":account};
    }else{
        userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result],@"deviceAccount":account};
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_DEVICE_RUN_RECORD_13001 object:nil userInfo:userData];
    return result;
}

+ (int) responseDeviceUserOpes:(GBInterMaster_NetData*)netData{
    if (netData.bodyData.length < 19) {
        return 0x01;
    }
    int offset = 0;
    Byte* bys = (Byte*)[netData.bodyData bytes];
    int result = bys[offset];
    offset += 1;
    NSString* account = [[NSString alloc] initWithBytes:bys+offset length:16 encoding:NSUTF8StringEncoding];
    offset += 16;
    int tag = bys[offset] != 0x00 ? 0xff : 0x00;
    offset += 1;
    int count = bys[offset];
    offset += 1;
    GBInterMaster_Bean_UserOpes13001* userOpe = nil;
    unsigned long tm = 0;
    NSMutableArray *opeRecords = [NSMutableArray arrayWithCapacity:0];
    if(result == 0x00){
        @try {
            for (int i = 0; i < count; ++i) {
                userOpe = [[GBInterMaster_Bean_UserOpes13001 alloc] init];
                userOpe.deviceType=DEVICE_TYPE_AMPUX_GM8_13001;
                tm = (long)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00)+ ((bys[offset + 2] & 0x0000FF) << 16 & 0xFF0000)+ ((bys[offset + 3] & 0x000000FF) << 24 & 0xFF000000));
                offset += 4;
                userOpe.requestTime = tm;
                tm = (long)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00)+ ((bys[offset + 2] & 0x0000FF) << 16 & 0xFF0000)+ ((bys[offset + 3] & 0x000000FF) << 24 & 0xFF000000));
                offset += 4;
                userOpe.responseTime = tm;
                userOpe.result = bys[offset];
                offset += 1;
                userOpe.type = bys[offset];
                offset += 1;
                if(userOpe.type == 1){
                    userOpe.workMode = bys[offset];
                    offset += 1;
                }else if(userOpe.type == 2){
                    userOpe.currentWorkMode = userOpe.workMode = bys[offset];
                    offset += 1;
                    userOpe.currentSetTempe = (short)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00));
                    offset += 2;
                }else  if(userOpe.type == 3){
                    userOpe.programDay = userOpe.workMode = bys[offset];
                    offset += 1;
                    userOpe.programOnePoint = (bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
                    offset += 2;
                    userOpe.programOneValue = (short)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00));
                    offset += 2;
                    userOpe.programTwoPoint = (bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
                    offset += 2;
                    userOpe.programTwoValue = (short)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00));
                    offset += 2;
                    userOpe.programThreePoint = (bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
                    offset += 2;
                    userOpe.programThreeValue = (short)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00));
                    offset += 2;
                    userOpe.programFourPoint = (bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
                    offset += 2;
                    userOpe.programFourValue = (short)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00));
                    offset += 2;
                }else if(userOpe.type == 4){
                    userOpe.powerSwitch = userOpe.workMode = bys[offset];
                    offset += 1;
                }else if(userOpe.type == 13){
                    userOpe.heatMode = userOpe.workMode = bys[offset];
                    offset += 1;
                }
                [opeRecords addObject:userOpe];
            }
        }
        @catch (NSException *exception) {
            result = 0x01;
        }
    }
    NSDictionary* userData;
    if(result == 0x00){
        userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result],@"tag":[NSNumber numberWithInt:tag],@"opeRecords":opeRecords,@"deviceAccount":account};
    }else{
        userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result],@"deviceAccount":account};
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_USER_OPERATE_RECORD_13001 object:nil userInfo:userData];
    return result;
}

+ (int) responseShowProgramInfo:(GBInterMaster_NetData*)netData{
    if (netData.bodyData.length < 17) {
        return 0x01;
    }
    int offset = 0;
    Byte* bys = (Byte*)[netData.bodyData bytes];
    int result = bys[offset];
    offset += 1;
    NSString* account = [[NSString alloc] initWithBytes:bys+offset length:16 encoding:NSUTF8StringEncoding];
    offset += 16;
    GBInterMaster_Bean_ProgramInfo* program = nil;
    NSMutableArray *programs = [NSMutableArray arrayWithCapacity:0];
    if(result == 0x00){
        @try {
            short top = (short)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00));
            offset += 2;
            short low = (short)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00));
            offset += 2;
            int i = 1;
            while (offset < netData.bodyData.length) {
                program = [[GBInterMaster_Bean_ProgramInfo alloc] init];
                program.weekDay = i;
                program.deviceType=DEVICE_TYPE_AMPUX_GM8_13001;
                program.deviceAccount = account;
                program.topValue = top;
                program.lowValue = low;
                program.onePoint = (bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
                offset += 2;
                program.oneValue = (short)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00));
                offset += 2;
                program.twoPoint = (bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
                offset += 2;
                program.twoValue = (short)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00));
                offset += 2;
                program.threePoint = (bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
                offset += 2;
                program.threeValue = (short)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00));
                offset += 2;
                program.fourPoint = (bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
                offset += 2;
                program.fourValue = (short)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00));
                offset += 2;
                [programs addObject:program];
            }
        }
        @catch (NSException *exception) {
            result = 0x01;
        }
    }
    NSDictionary* userData;
    if(result == 0x00){
        userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result],@"programs":programs,@"deviceAccount":account};
    }else{
        userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result],@"deviceAccount":account};
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_DEVICE_PROGRAM_13001 object:nil userInfo:userData];
    return result;
}


+ (int) reponseRunningState:(GBInterMaster_NetData*)netData{
    if(netData.bodyData.length != 51){
        NSDictionary* userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:0x01]};
        [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_DEVICE_STATUS_13001 object:nil userInfo:userData];
        return 0x01;
    }
    int offset = 0;
    Byte* bys = (Byte*)[netData.bodyData bytes];
    int result = bys[offset];
    if(result != 0x00){
        NSDictionary* userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result]};
        [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_DEVICE_STATUS_13001 object:nil userInfo:userData];
        return result;
    }
    offset += 1;
    GBInterMaster_Bean_DeviceInfo13001* device = [[GBInterMaster_Bean_DeviceInfo13001 alloc] init];
    device.isOnLine = true;
    device.account  = [[NSString alloc] initWithBytes:bys+offset length:16 encoding:NSUTF8StringEncoding];
    offset += 16;
    unsigned long tm = (long)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00)+ ((bys[offset + 2] & 0x0000FF) << 16 & 0xFF0000)+ ((bys[offset + 3] & 0x000000FF) << 24 & 0xFF000000));
    offset += 4;
    device.stateTime = tm;
    device.powerSwitch = bys[offset] & 0x00FF;
    offset += 1;
    device.workMode = bys[offset] & 0x00FF;
    offset += 1;
    device.isHeat = (bys[offset] & 0x00FF) == 1?true:false;
    offset += 1;
    device.heatMode = bys[offset] & 0x00FF;
    offset += 1;
    device.pragramCurrentPoint = bys[offset] & 0x00FF;
    offset += 1;
    device.currentRoomTempe =(bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
    offset += 2;
    device.currentRoomHumidity =(bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
    offset += 2;
    device.currentSetTempe =(bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
    offset += 2;
    device.QKHSetTemp =(bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
    offset += 2;
    device.CFTSetTemp =(bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
    offset += 2;
    device.ECSetTemp =(bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
    offset += 2;
    device.ProgramSetTemp =(bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
    offset += 2;
    device.tempSetLimitTop =(bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
    offset += 2;
    device.tempSetLimitLown =(bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
    offset += 2;
    tm = (long)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00)+ ((bys[offset + 2] & 0x0000FF) << 16 & 0xFF0000)+ ((bys[offset + 3] & 0x000000FF) << 24 & 0xFF000000));
    offset += 4;
    device.actionTotalTime = tm;
    device.isSiliconControlProtection = (bys[offset] & 0x00FF) == 1?true:false;
    offset += 1;
    device.isOverTempProtection = (bys[offset] & 0x00FF) == 1?true:false;
    offset += 1;
    device.isColdTempProtection = (bys[offset] & 0x00FF) == 1?true:false;
    offset += 1;
    NSDictionary* userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result],@"device":device};
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_DEVICE_STATUS_13001 object:nil userInfo:userData];
    return 0x00;
}

+ (int) pushRunningState:(GBInterMaster_NetData*)netData{
    if(netData.bodyData.length != 50){
        return 0x01;
    }
    GBInterMaster_Bean_DeviceInfo13001* device = [[GBInterMaster_Bean_DeviceInfo13001 alloc] init];
    device.isOnLine = true;
    int offset = 0;
    Byte* bys = (Byte*)[netData.bodyData bytes];
    device.deviceType =(bys[offset] & 0x00FF)  + ((bys[offset+1] & 0x00FF) << 8 & 0xFF00);
    offset += 2;
    device.account = [[NSString alloc] initWithBytes:bys+offset length:16 encoding:NSUTF8StringEncoding];
    offset += 16;
    unsigned long tm = (long)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00)+ ((bys[offset + 2] & 0x0000FF) << 16 & 0xFF0000)+ ((bys[offset + 3] & 0x000000FF) << 24 & 0xFF000000));
    offset += 4;
    device.stateTime = tm;
    device.powerSwitch = bys[offset] & 0x00FF;
    offset += 1;
    device.workMode = bys[offset] & 0x00FF;
    offset += 1;
    device.isHeat = (bys[offset] & 0x00FF) == 1?true:false;
    offset += 1;
    device.heatMode = bys[offset] & 0x00FF;
    offset += 1;
    device.pragramCurrentPoint = bys[offset] & 0x00FF;
    offset += 1;
    device.currentRoomTempe =(bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
    offset += 2;
    device.currentRoomHumidity =(bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
    offset += 2;
    device.currentSetTempe =(bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
    offset += 2;
    device.QKHSetTemp =(bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
    offset += 2;
    device.CFTSetTemp =(bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
    offset += 2;
    device.ECSetTemp =(bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
    offset += 2;
    device.ProgramSetTemp =(bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
    offset += 2;
    device.tempSetLimitTop =(bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
    offset += 2;
    device.tempSetLimitLown =(bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00);
    offset += 2;
    tm = (long)((bys[offset] & 0x00FF)  + ((bys[offset + 1] & 0x00FF) << 8 & 0xFF00)+ ((bys[offset + 2] & 0x0000FF) << 16 & 0xFF0000)+ ((bys[offset + 3] & 0x000000FF) << 24 & 0xFF000000));
    offset += 4;
    device.actionTotalTime = tm;
    int tag = bys[offset] & 0x00FF;
    offset += 1;
    device.isSiliconControlProtection = (tag & 0x01) == 1?true:false;
    device.isOverTempProtection = (tag >> 1 & 0xFF) == 1?true:false;
    device.isColdTempProtection = (tag >> 2 & 0xFF) == 1?true:false;
    device.workModeSetTime = device.heatModeSetTime = device.powerSwitchSetTime = device.currentSetTempeSetTime = [SampuxGetCurrentTime getCurrentTimeWithDate];
    NSDictionary* userData = @{@"device":device};

    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_PUSH_DEVICE_STATUS_13001 object:nil userInfo:userData];
    return 0x00;
}

+ (int) requestPowerSwitch:(int)msgNumber withDataVersion:(int)dataVersion withDeviceAccount:(NSString*)deviceAccount withPowerSwitch:(int)powerSwitch withService:(GBInterMaster_Launcher*)service{
    NSData* device_account_dt = [deviceAccount dataUsingEncoding:NSUTF8StringEncoding];
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.dataType = PTCP_V10_DATA_TYPE_SELF;
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_DEVICE;
    packData.cmd = LOGIC_CMD_REQUEST_POWERSWITCH;
    packData.connId = service.serverConnID;
    packData.dataVersion = dataVersion ;
    packData.msgId = msgNumber;
    Byte reqs[17];
    memset(reqs, 0x00, 17);
    memcpy(reqs, [device_account_dt bytes] , device_account_dt.length < 16 ? device_account_dt.length:16);
    reqs[16] = powerSwitch;
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:17];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:msgNumber];
    }
    return 0;

}

+ (int) requestWorkMode:(int)msgNumber withDataVersion:(int)dataVersion withDeviceAccount:(NSString*)deviceAccount withWorkMode:(int)workMode withService:(GBInterMaster_Launcher*)service{
    NSData* device_account_dt = [deviceAccount dataUsingEncoding:NSUTF8StringEncoding];
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.dataType = PTCP_V10_DATA_TYPE_SELF;
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_DEVICE;
    packData.cmd = LOGIC_CMD_REQUEST_WORKMODEL;
    packData.connId = service.serverConnID;
    packData.dataVersion = dataVersion ;
    packData.msgId = msgNumber;
    Byte reqs[17];
    memset(reqs, 0x00, 17);
    memcpy(reqs, [device_account_dt bytes] , device_account_dt.length < 16 ? device_account_dt.length:16);
    reqs[16] = workMode;
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:17];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:msgNumber];
    }
    return 0;
}

+ (int) requestHeatMode:(int)msgNumber withDataVersion:(int)dataVersion withDeviceAccount:(NSString*)deviceAccount withHeatMode:(int)heatMode withService:(GBInterMaster_Launcher*)service{
    NSData* device_account_dt = [deviceAccount dataUsingEncoding:NSUTF8StringEncoding];
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.dataType = PTCP_V10_DATA_TYPE_SELF;
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_DEVICE;
    packData.cmd = LOGIC_CMD_REQUEST_HEATMODE;
    packData.connId = service.serverConnID;
    packData.dataVersion = dataVersion ;
    packData.msgId = msgNumber;
    Byte reqs[17];
    memset(reqs, 0x00, 17);
    memcpy(reqs, [device_account_dt bytes] , device_account_dt.length < 16 ? device_account_dt.length:16);
    reqs[16] = heatMode;
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:17];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:msgNumber];
    }
    return 0;
}

+ (int) requestSetTempe:(int)msgNumber withDataVersion:(int)dataVersion withDeviceAccount:(NSString*)deviceAccount withHeatMode:(int)heatMode withTempe:(short)tempe withService:(GBInterMaster_Launcher*)service{
    NSData* device_account_dt = [deviceAccount dataUsingEncoding:NSUTF8StringEncoding];
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.dataType = PTCP_V10_DATA_TYPE_SELF;
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_DEVICE;
    packData.cmd = LOGIC_CMD_REQUEST_TEMPEHM;
    packData.connId = service.serverConnID;
    packData.dataVersion = dataVersion ;
    packData.msgId = msgNumber;
    Byte reqs[19];
    memset(reqs, 0x00, 19);
    memcpy(reqs, [device_account_dt bytes] , device_account_dt.length < 16 ? device_account_dt.length:16);
    reqs[16] = heatMode;
    reqs[17] = tempe & 0xFF;
    reqs[18] = tempe >> 8 & 0xFF;
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:19];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:msgNumber];
    }
    return 0;
}

+ (int) requestSetProgram:(int)msgNumber withDataVersion:(int)dataVersion withDeviceAccount:(NSString*)deviceAccount withProgram:(GBInterMaster_Bean_ProgramInfo*)program withService:(GBInterMaster_Launcher*)service{
    NSData* device_account_dt = [deviceAccount dataUsingEncoding:NSUTF8StringEncoding];
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.dataType = PTCP_V10_DATA_TYPE_SELF;
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_DEVICE;
    packData.cmd = LOGIC_CMD_REQUEST_DAYPARTING;
    packData.connId = service.serverConnID;
    packData.dataVersion = dataVersion ;
    packData.msgId = msgNumber;
    int offset = 0;
    Byte reqs[33];
    memset(reqs, 0x00, 33);
    memcpy(reqs, [device_account_dt bytes] , device_account_dt.length < 16 ? device_account_dt.length:16);
    offset += 16;
    reqs[offset] = program.weekDay;
    offset += 1;
    reqs[offset] = program.onePoint & 0xFF;
    reqs[offset+1] = program.onePoint >> 8 & 0xFF;
    offset += 2;
    reqs[offset] = program.oneValue & 0xFF;
    reqs[offset+1] = program.oneValue >> 8 & 0xFF;
    offset += 2;
    
    reqs[offset] = program.twoPoint & 0xFF;
    reqs[offset+1] = program.twoPoint >> 8 & 0xFF;
    offset += 2;
    reqs[offset] = program.twoValue & 0xFF;
    reqs[offset+1] = program.twoValue >> 8 & 0xFF;
    offset += 2;
    
    reqs[offset] = program.threePoint & 0xFF;
    reqs[offset+1] = program.threePoint >> 8 & 0xFF;
    offset += 2;
    reqs[offset] = program.threeValue & 0xFF;
    reqs[offset+1] = program.threeValue >> 8 & 0xFF;
    offset += 2;
    
    reqs[offset] = program.fourPoint & 0xFF;
    reqs[offset+1] = program.fourPoint >> 8 & 0xFF;
    offset += 2;
    reqs[offset] = program.fourValue & 0xFF;
    reqs[offset+1] = program.fourValue >> 8 & 0xFF;
    offset += 2;
    
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:33];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:msgNumber];
    }
    return 0;
}
@end
